package cn.edu.ctbu.demo2.webapi;

import cn.edu.ctbu.demo2.domain.Student;
import cn.edu.ctbu.demo2.domain.User;
import cn.edu.ctbu.demo2.service.StudentService;
import cn.edu.ctbu.demo2.service.UserService;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/webapi/login")
public class LoginRestController {
    @Autowired
    UserService userService;

    @GetMapping("/user")
    public int user(HttpServletRequest httpServletRequest,String user,String password){
        List<User> users=userService.findByUserAndPassword(user,password);

        if(users.size()>0){
        HttpSession session=httpServletRequest.getSession();
        session.setAttribute("userid",users.get(0).getId());

        session.setAttribute("user",users.get(0));

        return 1;
    }else {
        return -1;
    }

}

}
